<?php
namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Option;
use think\Db;

class OrderStats extends Command
{
    protected function configure()
    {
        $this->setName('order:stats')
            ->setDescription('统计订单数据')
            ->addOption('date', 'd', Option::VALUE_OPTIONAL, '统计日期(Y-m-d格式)', '');
    }

    protected function execute(Input $input, Output $output)
    {
        $date = $input->getOption('date');
        
        // 如果没有传入日期
        if (empty($date)) {
            // 凌晨1点执行时统计前一天
            if (date('H') == '01') {
                $date = date('Y-m-d', strtotime('-1 day'));
            } else {
                // 其他时间统计当天
                $date = date('Y-m-d');
            }
        }
        
        $start = strtotime($date . ' 00:00:00');
        $end = strtotime($date . ' 23:59:59');

        try {
            Db::startTrans();
            
            // 文旅订单统计
            $travelStats = Db::name('travel_order')
                ->where('status', 1)
                ->whereBetween('pay_time', [$start, $end])
                ->field([
                    'COUNT(*) as order_count',
                    'SUM(total_price) as order_amount'
                ])
                ->find();
            
            // 使用REPLACE INTO替代删除再插入
            Db::execute('REPLACE INTO gu_travel_order_stats (`date`, `order_count`, `order_amount`) VALUES (?, ?, ?)', [
                $date,
                $travelStats['order_count'] ?: 0,
                $travelStats['order_amount'] ?: 0
            ]);

            // 商城订单统计
            $shopStats = Db::name('order')
                ->where('status', 1)
                ->whereBetween('pay_time', [$start, $end])
                ->field([
                    'COUNT(*) as order_count',
                    'SUM(order_amount) as order_amount'
                ])
                ->find();
            
            // 使用REPLACE INTO替代删除再插入
            Db::execute('REPLACE INTO gu_shop_order_stats (`date`, `order_count`, `order_amount`) VALUES (?, ?, ?)', [
                $date,
                $shopStats['order_count'] ?: 0,
                $shopStats['order_amount'] ?: 0
            ]);

            // 服务订单统计
            $serviceStats = Db::name('service_order')
                ->where('status', 1)
                ->whereBetween('pay_time', [$start, $end])
                ->field([
                    'COUNT(*) as order_count',
                    'SUM(amount) as order_amount'
                ])
                ->find();
            
            // 使用REPLACE INTO替代删除再插入
            Db::execute('REPLACE INTO gu_service_order_stats (`date`, `order_count`, `order_amount`) VALUES (?, ?, ?)', [
                $date,
                $serviceStats['order_count'] ?: 0,
                $serviceStats['order_amount'] ?: 0
            ]);

            Db::commit();
            $output->writeln("订单统计完成: {$date}");
            
        } catch (\think\exception\PDOException $e) {
            Db::rollback();
            $output->writeln("订单统计失败: " . $e->getMessage());
        }
    }
}